/**
 * @description 根据WMS服务的能力文档地址获取关键参数
 * @param {string} wmsURL WMS服务的能力文档信息
 * @returns {Promise<Object>} 包含WMS服务的关键参数的Promise对象
 */
export default class WMS {
  #wmsInfoDoc = null
  #wmsType = ''
  #provider = ''
  #getCapabilitiesURL = ''
  #getMapURL = ''
  #layer = []

  constructor(wmsInfo) {
    this.#wmsInfoDoc = new DOMParser().parseFromString(wmsInfo, 'text/xml')
    this.#provider = this.#wmsInfoDoc.querySelectorAll('ContactOrganization')[0] ? this.#wmsInfoDoc.querySelectorAll('ContactOrganization')[0].innerHTML : 'other'
    this.#wmsInfoDoc.querySelectorAll('WMT_MS_Capabilities').length !== 0 ? (this.#wmsType = '1.1.1') : (this.#wmsType = '1.3.0')
    this.#getCapabilitiesURL = this.#wmsInfoDoc.querySelector('GetCapabilities').querySelector('OnlineResource').getAttribute('xlink:href').split('?')[0]
    this.#getMapURL = this.#wmsInfoDoc.querySelector('GetMap').querySelector('OnlineResource').getAttribute('xlink:href').split('?')[0]
    this.#getLayerInfo()
  }

  get info() {
    return {
      wmsType: this.#wmsType,
      provider: this.#provider,
      getCapabilitiesURL: this.#getCapabilitiesURL,
      getMapURL: this.#getMapURL,
      layer: this.#layer
    }
  }

  #getLayerInfo() {
    const finds = this.#wmsInfoDoc.querySelectorAll('Layer')
    this.#layer.push({
      name: finds[1].querySelector('Name').textContent,
      title: finds[1].querySelector('Title').textContent,
      boundingBox: {
        srs: finds[1].querySelector('BoundingBox').getAttribute(this.#wmsType === '1.1.1' ? 'SRS' : 'CRS'),
        minx: finds[1].querySelector('BoundingBox').getAttribute('minx'),
        miny: finds[1].querySelector('BoundingBox').getAttribute('miny'),
        maxx: finds[1].querySelector('BoundingBox').getAttribute('maxx'),
        maxy: finds[1].querySelector('BoundingBox').getAttribute('maxy')
      },
      LatLonBoundingBox: {
        minx: this.#wmsType === '1.1.1' ? finds[1].querySelector('LatLonBoundingBox').getAttribute('minx') : finds[1].querySelector('westBoundLongitude').textContent,
        miny: this.#wmsType === '1.1.1' ? finds[1].querySelector('LatLonBoundingBox').getAttribute('miny') : finds[1].querySelector('southBoundLatitude').textContent,
        maxx: this.#wmsType === '1.1.1' ? finds[1].querySelector('LatLonBoundingBox').getAttribute('maxx') : finds[1].querySelector('eastBoundLongitude').textContent,
        maxy: this.#wmsType === '1.1.1' ? finds[1].querySelector('LatLonBoundingBox').getAttribute('maxy') : finds[1].querySelector('northBoundLatitude').textContent
      }
    })
    const subLayerNode = finds[1].querySelectorAll('Layer')
    subLayerNode.forEach((layer) => {
      this.#layer.push({
        name: layer.querySelector('Name').textContent,
        title: layer.querySelector('Title').textContent,
        boundingBox: {
          srs: layer.querySelector('BoundingBox').getAttribute(this.#wmsType === '1.1.1' ? 'SRS' : 'CRS'),
          minx: layer.querySelector('BoundingBox').getAttribute('minx'),
          miny: layer.querySelector('BoundingBox').getAttribute('miny'),
          maxx: layer.querySelector('BoundingBox').getAttribute('maxx'),
          maxy: layer.querySelector('BoundingBox').getAttribute('maxy')
        },
        LatLonBoundingBox: {
          minx: this.#wmsType === '1.1.1' ? finds[1].querySelector('LatLonBoundingBox').getAttribute('minx') : finds[1].querySelector('westBoundLongitude').textContent,
          miny: this.#wmsType === '1.1.1' ? finds[1].querySelector('LatLonBoundingBox').getAttribute('miny') : finds[1].querySelector('southBoundLatitude').textContent,
          maxx: this.#wmsType === '1.1.1' ? finds[1].querySelector('LatLonBoundingBox').getAttribute('maxx') : finds[1].querySelector('eastBoundLongitude').textContent,
          maxy: this.#wmsType === '1.1.1' ? finds[1].querySelector('LatLonBoundingBox').getAttribute('maxy') : finds[1].querySelector('northBoundLatitude').textContent
        }
      })
    })
  }
}
